//
//  MyopiaReserveHospitalViewModel.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2021/1/6.
//  Copyright © 2021 QDSG. All rights reserved.
//

import Foundation
import HandyJSON

struct CheckProjectModel: HandyJSON {
    /// 检查项目id
    var id: String = ""
    /// 检查项目code
    var code: String = ""
    /// 检查项目名称
    var name: String = ""
    /// 是否选择
    var selected: Bool = false
}

struct MyopiaHospitalModel: HandyJSON {
    /// 医院id
    var hosId: String = ""
    /// 医院名称
    var hosName: String = ""
    /// 医院地址
    var hosAddr: String = ""
    /// 检查项目数组
    var checkLists: [CheckProjectModel] = []
}

struct MyopiaHospitalViewModel {
    var models: [MyopiaHospitalModel] = []
    
    var numberOfSection: Int {
        return models.count
    }
    
    func hospitalModel(indexPath: IndexPath) -> MyopiaHospitalModel {
        return models[indexPath.section]
    }
    
    /// 获取医院列表列表 hosDeviceId: 医院设备id, hosName: 模糊搜索医院名
    static func getMyopiaHospitalList(hosDeviceId: String = "", hosName: String = "", finished: @escaping ([MyopiaHospitalModel], String?, HttpCode) -> Void) {
        let params = ["hosDeviceId": hosDeviceId, "hosName": hosName]
        HttpTool.shared.request(target: ApiManager.myopiaHospitalList(paras: params)) { (json, code) in
            if let models = JSONDeserializer<MyopiaHospitalModel>.deserializeModelArrayFrom(json: json["data"].description) as? [MyopiaHospitalModel] {
                    finished(models, nil, code)
                } else {
                    finished([], "请求数据发生错误，请重试!", .failure)
                }
        } failure: { error in
            finished([], error, .failure)
        }
    }
}
